package airport.page.action;

import java.util.LinkedList;

import airport.forum.pojo.Forum;
import airport.forum.pojo.Thread;
import airport.forum.dao.ForumDAO;

import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
//index
public class IndexAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
	
	private static final long serialVersionUID = 1L;
	
	private String username = null;
	private String forumName = null;
	private long threadCount;
	private LinkedList<Thread> threads = null;
	
	protected HttpServletResponse servletResponse;  
	@Override   
	public void setServletResponse(HttpServletResponse servletResponse) {   
		this.servletResponse = servletResponse;   
	}   
	
	protected HttpServletRequest servletRequest;   
	@Override   
	public void setServletRequest(HttpServletRequest servletRequest) {   
		this.servletRequest = servletRequest;   
	}
	
	public String execute() {
		//find cookie
		System.out.println("This is Index Action");
		String cookieName = "AirportUser";
		Cookie cookies[] = servletRequest.getCookies();
		//System.out.println("going to find cookie");
		
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals(cookieName)) {
					username = cookies[i].getValue();
					break;
				}
			}
		}
		
		//get the forum information
		ForumDAO dao =  new ForumDAO();
		Forum forum = dao.getForum((long)1);
		forumName = forum.getForumName();
		threadCount = forum.getThreadCount();
		
		//get the threads
		threads = new LinkedList<Thread> ();
		threads = dao.getThreadsByPage((long)1, 1, 10);
		
		
		return "success";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getForumName() {
		return forumName;
	}

	public void setForumName(String forumName) {
		this.forumName = forumName;
	}

	public LinkedList<Thread> getThreads() {
		return threads;
	}

	public void setThreads(LinkedList<Thread> threads) {
		this.threads = threads;
	}

	public long getThreadCount() {
		return threadCount;
	}

	public void setThreadCount(long threadCount) {
		this.threadCount = threadCount;
	}



}
